//
//  UITableView+Extend.m
//  IosTemplate
//
//  Created by GuoliWang on 2018/8/24.
//  Copyright © 2018年 GuoliWang. All rights reserved.
//

#import "UITableView+Extend.h"

@implementation UITableView (Extend)

/**
 初始化自适应cell的tableview
 
 @param frame frame
 @param style Plain or Grouped
 @param delegateAndDataSource 代理和数据源
 @return tableview
 */
+ (UITableView *)estimatedLayoutTableview:(CGRect)frame tableviewStyle:(UITableViewStyle )style delegateAndDataSource:(id)delegateAndDataSource{
    return [UITableView estimatedLayoutTableview:frame tableviewStyle:style delegateAndDataSource:delegateAndDataSource estimatedSectionHeader:NO estimatedSectionFooter:NO];
}

+ (UITableView *)estimatedLayoutTableview:(CGRect)frame tableviewStyle:(UITableViewStyle )style delegateAndDataSource:(id)delegateAndDataSource estimatedSectionHeader:(BOOL)estimatedSectionHeader estimatedSectionFooter:(BOOL)estimatedSectionFooter{
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:style];
    
    if (delegateAndDataSource) {
        tableView.delegate = delegateAndDataSource;
        tableView.dataSource = delegateAndDataSource;
    }
    //自动cell
    tableView.estimatedRowHeight = 100;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //自动sectionHeader
    if (estimatedSectionHeader){
        tableView.estimatedSectionHeaderHeight = UITableViewAutomaticDimension;
        tableView.sectionHeaderHeight = 50;
    }
    
    
    //自动sectionFooter
    if (estimatedSectionFooter){
        tableView.estimatedSectionFooterHeight = UITableViewAutomaticDimension;
        tableView.sectionFooterHeight = 50;
    }
    
//    tableView.tableFooterView = [[UIView alloc]init];
    return tableView;
}

//UITableView刷新完成
- (void)reloadDataCompletion:(void (^)(void))completion{
    [self reloadData];
    [self setNeedsLayout];
    [self layoutIfNeeded];
    // reloadDate会在主队列执行，而dispatch_get_main_queue会等待机会，直到主队列空闲才执行。
    dispatch_async(dispatch_get_main_queue(), ^{
            //刷新完成
            if (completion) {
                completion();
            }
    });

}

- (void)updateWithBlock:(void (^)(UITableView *tableView))block {
    [self beginUpdates];
    block(self);
    [self endUpdates];
}

- (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    [self scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}

- (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self insertRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

- (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toInsert = [NSIndexPath indexPathForRow:row inSection:section];
    [self insertRowAtIndexPath:toInsert withRowAnimation:animation];
}

- (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

- (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toReload = [NSIndexPath indexPathForRow:row inSection:section];
    [self reloadRowAtIndexPath:toReload withRowAnimation:animation];
}

- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

- (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toDelete = [NSIndexPath indexPathForRow:row inSection:section];
    [self deleteRowAtIndexPath:toDelete withRowAnimation:animation];
}

- (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
    [self insertSections:sections withRowAnimation:animation];
}

- (void)deleteSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
    [self deleteSections:sections withRowAnimation:animation];
}

- (void)reloadSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
    [self reloadSections:indexSet withRowAnimation:animation];
}

- (void)clearSelectedRowsAnimated:(BOOL)animated {
    NSArray *indexs = [self indexPathsForSelectedRows];
    [indexs enumerateObjectsUsingBlock:^(NSIndexPath* path, NSUInteger idx, BOOL *stop) {
        [self deselectRowAtIndexPath:path animated:animated];
    }];
}
@end
